package com.pangdonghao.internationalizationdemo.core.response;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 返回值封装
 * @author pangdonghao
 * @csdn https://blog.csdn.net/pangdongh
 * @version 1.0
 * @createDate 2019/08/19 13:52
 */
@Api(value = "返回值封装")
@Data
public class Result<T> implements Serializable {

    /**
     * @Fields serialVersionUID
     */
    private static final long serialVersionUID = 2325646055961350652L;
    @ApiModelProperty(value = "响应状态")
    private Meta meta;


    /**
     * 响应中的数据
     */
    @ApiModelProperty(value = "响应数据", name = "data")
    private T data;

    /**
     * 数据条数
     */
    @ApiModelProperty(value = "数据条数", name = "data", hidden = true)
    private Long totalCount;

    /**
     * 数据页码
     */
    @ApiModelProperty(value = "响应数据", name = "data", hidden = true)
    private Integer totalPage;
    /**
     * 成功状态码
     */
    public static final Integer SUCCESS_CODE = 200;
    /**
     * 成功描述
     */
    public static final String SUCCESS_MSG = "操作成功";
    /**
     * 失败状态码
     */
    public static final Integer ERROR_CODE = 500;
    /**
     * 失败描述
     */
    public static final String ERROR_MSG = "操作失败";
    /**
     * 失败状态码
     */
    public static final Integer ERROR_PARAM_CODE = 2;
    /**
     * 失败描述
     */
    public static final String ERROR_PARAM_MSG = "参数有误";

    public static Result success(Object data, Integer totalPage, Long totalCount) {
        Result result = new Result(SUCCESS_CODE, SUCCESS_MSG, data);
        result.setTotalPage(totalPage);
        result.setTotalCount(totalCount);
        return result;
    }


    public static <T> Result<T> success(T data) {
        return new Result(SUCCESS_CODE, SUCCESS_MSG, data);
    }

    public static Result success() {
        return new Result(SUCCESS_CODE, SUCCESS_MSG, null);
    }

    public static Result error(Object data) {
        return new Result(ERROR_CODE, ERROR_MSG, data);
    }
    public static Result error(String data) {
        return new Result(ERROR_CODE, data, data);
    }
    public static Result error(Meta meta) {
        return new Result(meta);
    }
    public static Result error() {
        return new Result(ERROR_CODE, ERROR_MSG, null);
    }
    public static Result error(int code, String message) {
        return new Result(code, message, null);
    }

    public static Result paramError(Object data){
        return build(ERROR_PARAM_CODE,ERROR_PARAM_MSG,data);
    }

    public static Result build(Integer status, String msg, Object data) {
        return new Result(status, msg, data);
    }

    public static Result build(Integer status, String msg) {
        return new Result(status, msg, null);
    }

    public static Result build(Meta meta) {
        return new Result(meta, null);
    }

    public Result() {

    }

    public Result(Integer status, String msg, T data) {
        if(status==200){//成功
            this.meta = new Meta(status,true, msg);
        }else {
            this.meta = new Meta(status,false, msg);
        }
        this.data = data;
    }

    public Result(Meta meta, T data) {
        this.meta = meta;
        this.data = data;
    }
    public Result(Meta meta) {
        this.meta = meta;
    }
}
